<!-- 菜单弹框 -->
<form id="menus-form" lay-filter="menus-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单名称</label>
        <div class="layui-input-block">
            <input name="title" placeholder="请输入菜单名称" type="text" class="layui-input" maxlength="50"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单URL</label>
        <div class="layui-input-block">
            <input name="url" placeholder="请输入菜单URL" type="text" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单PATH</label>
        <div class="layui-input-block">
            <input name="html" placeholder="请输入菜单PATH" type="text" class="layui-input"/>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input name="css" placeholder="请输入菜单图标" type="text" class="layui-input"/>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">是否隐藏</label>
        <div class="layui-input-block">
            <input type="radio" name="hidden" value="true" title="是"/>
            <input type="radio" name="hidden" value="false" title="否" checked/>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">外链菜单</label>
        <div class="layui-input-block">
            <input type="radio" name="iframe" value="true" title="是"/>
            <input type="radio" name="iframe" value="false" title="否" checked/>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">是否为菜单</label>
        <div class="layui-input-block">
            <input type="radio" name="type" lay-filter="resourceType" value="1" title="是" checked/>
            <input type="radio" name="type" lay-filter="resourceType" value="2" title="否"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">请求方法</label>
        <div class="layui-input-block">
            <select name="pathMethod" id="pathMethod" disabled="true">
                <option value="-1"></option>
                <option>GET</option>
                <option>POST</option>
                <option>DELETE</option>
                <option>PUT</option>
            </select>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input name="menuSort" placeholder="请输入排序号" type="number" class="layui-input" lay-verify="required" required/>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">上级菜单</label>
        <div class="layui-input-block">
            <select name="pid" id="pid">
                <option value="-1">顶级目录</option>
            </select>
        </div>
    </div>
    
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="menus-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
layui.use(['layer', 'admin', 'form', 'formSelects'],function () {
    var layer = layui.layer;
    var admin = layui.admin;
    var form = layui.form;
    var formSelects = layui.formSelects;
    
    // 修改菜单类型
    let changeType = function (type) {
        if (type == 2) {
            $('#pathMethod').attr('disabled', false);
        } else {
            $('#pathMethod').attr('disabled', true);
            $("#pathMethod").val("");
        }
        form.render('select', 'menus-form');
    };
    
    // 回显menu数据
    let menus = admin.getTempData('t_menus');
    // 获取一级菜单
    layer.load(2);
    admin.req('api-user/menus/findOnes', {tenantId: menus.tenantId}, function (data) {
        layer.closeAll('loading');
        if (0 == data.code) {
            $.each(data.data,function(index,item){
                $('#pid').append(new Option(item.title,item.id));//往下拉菜单里添加元素
            })
            $('#menus-form').attr('method', 'POST');
            if (menus) {
                form.val('menus-form', menus);
                changeType(menus.type);
            }
            form.render();  //菜单渲染 把内容加载进去
        } else {
            layer.msg('获取一级菜单', {icon: 2, time: 500});
        }
    }, 'GET');
    
    // 表单提交事件
    form.on('submit(menus-form-submit)', function (data) {
        layer.load(2);
        if (data.field["pid"] === "-1") {
            data.field["type"] = "0";
        }
        admin.req('api-user/menus/saveOrUpdate?tenantId='+menus.tenantId, JSON.stringify(data.field), function (data) {
            if (data.resp_code === 0) {
                layer.closeAll('loading');
                layer.msg(data.resp_msg, {icon: 1, time: 500});
                admin.finishPopupCenter();
            } else {
                layer.closeAll('loading');
                layer.msg(data.resp_msg, {icon: 2, time: 500});
            }
        }, $('#menus-form').attr('method'));
        return false;
    });

    form.on("radio(resourceType)", function (data) {
        changeType(data.value);
    });
});
</script>